﻿package com.designoutput.ui.newControls
{
	import com.designoutput.enum.ToolType;
	import com.designoutput.icon.DragMark;
	import com.designoutput.icon.WinCloseOn;
	import com.designoutput.icon.WindowBottomLeft;
	import com.designoutput.icon.WindowBottomMiddle;
	import com.designoutput.icon.WindowBottomRight;
	import com.designoutput.icon.WindowMiddleLeft;
	import com.designoutput.icon.WindowMiddleMiddle;
	import com.designoutput.icon.WindowMiddleRight;
	import com.designoutput.icon.WindowTopLeft;
	import com.designoutput.icon.WindowTopMiddle;
	import com.designoutput.icon.WindowTopRight;
	import com.designoutput.ui.controls.IconButton;
	import com.designoutput.utils.Globals;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Window extends ControlBase
	{
		protected var tr:Bitmap;
		protected var ml:Bitmap;
		protected var mr:Bitmap;
		protected var mm:Bitmap;
		protected var bm:Bitmap;
		protected var br:Bitmap;
		protected var bl:Bitmap;
		protected var tl:Bitmap;
		protected var tm:Bitmap;
		
		private var _titleTextField:TextField;	
		protected var closeButton:IconButton;
		private var _showCloseButton:Boolean = false;
		protected var windowContent:Sprite;
		
		protected var drag:Sprite;
		protected var grip:Sprite;
		public var zoomer:Sprite;
		
		public var back:Sprite;
		
		private var _gripDragging:Boolean = false;
		
		private var _resizeStartMousePoint:Point;
		private var _resizeStartSize:Point;
		
		override protected function initialize():void {
			super.initialize();
			
			tl = new Bitmap(new WindowTopLeft(0, 0));
			addChild(tl);
			tm = new Bitmap(new WindowTopMiddle(0, 0));
			addChild(tm);
			tr = new Bitmap(new WindowTopRight(0, 0));
			addChild(tr);
			ml = new Bitmap(new WindowMiddleLeft(0, 0));
			addChild(ml);
			mm = new Bitmap(new WindowMiddleMiddle(0, 0));
			addChild(mm);
			mr = new Bitmap(new WindowMiddleRight(0, 0));
			addChild(mr);
			bl = new Bitmap(new WindowBottomLeft(0, 0));
			addChild(bl);
			bm = new Bitmap(new WindowBottomMiddle(0, 0));
			addChild(bm);
			br = new Bitmap(new WindowBottomRight(0, 0));
			addChild(br);

			_titleTextField = new TextField();
			_titleTextField.defaultTextFormat = new TextFormat("Verdana,Helvetica,San-serif", 12, 0xFFFFFF, false, false, false, null, null, "center");
			_titleTextField.multiline = false;
			_titleTextField.selectable = false;
			_titleTextField.y = 2;
			_titleTextField.height = 18;
			addChild(_titleTextField);

			drag = new Sprite();
			drag.graphics.beginFill(0x909090);
			drag.graphics.drawRect(0, 0, 100, 100);
			drag.graphics.endFill();
			drag.alpha = 0;
			drag.addEventListener(MouseEvent.MOUSE_DOWN, dragMouseDown, false, 0, true);
			drag.addEventListener(MouseEvent.MOUSE_UP, dragMouseUp, false, 0, true);
			addChild(drag);
									
			windowContent = new Sprite();
			windowContent.x = 4;
			windowContent.y = 22;
			addChild(windowContent);
			
			grip = new Sprite();
			grip.addEventListener(MouseEvent.MOUSE_OVER, gripMouseOver, false, 0, true);
			grip.addEventListener(MouseEvent.MOUSE_OUT, gripMouseOut, false, 0, true);
			grip.addEventListener(MouseEvent.MOUSE_DOWN, gripMouseDown, false, 0, true);
			grip.graphics.beginFill(0xB5B7B9);
			grip.graphics.drawRect(0, 0, 16, 16);
			grip.graphics.endFill();
			var _dragMark:Bitmap = new Bitmap(new DragMark(0, 0));
			_dragMark.x = 4;
			_dragMark.y = 4;
			grip.addChild(_dragMark);
			addChild(grip);
			
			closeButton = new IconButton(new WinCloseOn(0, 0));
			addChild(closeButton);
		}
						
		override protected function updateDisplay():void {
			super.updateDisplay();
				
			tm.width = mm.width = bm.width = _width - 64;
			ml.height = mm.height = mr.height = _height - 104;
			tm.x = mm.x = bm.x = 32;
			tr.x = mr.x = br.x = tm.width + 32;
			ml.y = mm.y = mr.y = 100;
			br.y = bm.y = bl.y = mm.height + 100;
			
			_titleTextField.width = _width;
			
			drag.width = _width - 2;
			drag.height = 17;
			
			closeButton.y = 4;
			closeButton.x = _width - 21;
			
			grip.x = _width - 20;
			grip.y = _height - 20;
		}
		
		override protected function get minWidth():Number {
			return 250;
		}
		
		override protected function get minHeight():Number {
			return 150;
		}
		
		protected function get contentAvailableWidth():Number {
			return _width - 8;
		}
		
		protected function get contentAvailableHeight():Number {
			return _height - 26;
		}
		
		/**
		 * Called when end user click the "Close" button of this window
		 * @param event
		 *
		 */
		public function CloseClick(event:MouseEvent):void
		{
			//ed.cm.ConfirmClose();
		}

		public function setTitle(param1:String):void
		{
			_titleTextField.text = param1;
		}

		/**
		 * drag to move the window
		 * @param event
		 */
		public function dragMouseDown(event:MouseEvent):void
		{
			startDrag(false, new Rectangle(-4000, 60, 8000, stage.stageHeight + 2000));
			stage.addEventListener(MouseEvent.MOUSE_UP, dragMouseUp, true, 0, true);
		}

		public function dragMouseUp(event:MouseEvent):void
		{
			stopDrag();
			if (stage != null)
			{
				stage.removeEventListener(MouseEvent.MOUSE_UP, dragMouseUp);
			}
		}

		public function gripMouseOver(event:MouseEvent):void
		{
			Globals.cursorManager.setCursor(ToolType.ResizeHeightWidth);
		}
		
		public function gripMouseOut(event:MouseEvent):void
		{
			Globals.cursorManager.setCursor(ToolType.None);
		}
		
		private function gripMouseDown(evt:MouseEvent):void
		{
			_resizeStartSize = new Point(width, height);
			_resizeStartMousePoint = new Point(mouseX, mouseY);
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, gripMouseMove, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, gripMouseUp, false, 0, true);
			
			Globals.cursorManager.lock();
		}
		
		private function gripMouseMove(evt:MouseEvent):void
		{
			var mousePoint:Point = new Point(mouseX, mouseY);
			
			var offset:Point = mousePoint.subtract(_resizeStartMousePoint);
			
			width = _resizeStartSize.x + offset.x;
			height = _resizeStartSize.y + offset.y;
		}
		
		private function gripMouseUp(evt:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, gripMouseMove, false);
			stage.removeEventListener(MouseEvent.MOUSE_UP, gripMouseUp, false);
			
			_resizeStartSize = null;
			_resizeStartMousePoint = null;
						
			Globals.cursorManager.unlock();
						
			if (evt.target != grip)
			{
				Globals.cursorManager.setCursor(ToolType.None);
				Globals.designoutputCard.resetTool();
			}
		}
	}
}
